/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Engine2D.graphics;

import Engine2D.engine.Engine;
import Engine2D.graphics.GLColour;
import Engine2D.graphics.GLTexture;
import java.awt.Dimension;
import java.awt.Image;

import org.lwjgl.opengl.GL11;

/**
 *
 * @author Garys
 */
public class GLSpriteSheet {

    /**
     * 
     */
    public void bind() {
        texture.bind();
    }
    
    private GLTexture texture = null;
    private final String filename;
    private int w;
    private int h;
    private int u;
    private int v;
    /**
     * 
     */
    public static GLSpriteSheet instance = null;
    
    /**
     * 
     * @return
     */
    public static GLSpriteSheet getInstance() {
        if (instance == null) {
            instance = new GLSpriteSheet(512,512);
        }
        return instance;
    }
    
    /** How many times we want to tile this image */
    private Dimension tilingValues = null;
    
    /** The color to use while drawing the image */
    private GLColour colour = GLColour.WHITE;

    /**
     * 
     * @param w
     * @param h
     */
    public GLSpriteSheet(int w, int h) {
        this.filename = "resources/images/Tiles/spriteSheet.png";
        texture = new GLTexture(filename, true);
        texture.bind();
        this.w = w;
        this.h = h;
        this.u = 0;
        this.v = 0;

        calculateTilingValues();
        
        

    }
 
    /**
     * 
     * @param mX
     * @param mY
     * @param mAlpha
     */
    public void draw(int mX, int mY, float mAlpha) {
        GLColour mColour =
                new GLColour(colour.getRed(), colour.getGreen(), colour.getBlue(),
                mAlpha);
        mColour.set();
        GL11.glEnable(GL11.GL_TEXTURE_2D);

        GL11.glDisable(GL11.GL_DEPTH_TEST);
        GL11.glEnable(GL11.GL_BLEND);

        texture.bind();
        GL11.glBegin(GL11.GL_QUADS);
        GL11.glNormal3f(0.0f, 0.0f, 1.0f);
        GL11.glTexCoord2f(0.0f, tilingValues.height);
        GL11.glVertex2i(mX, Engine.getInstance().getDisplayBounds().getY()
                - mY);
        GL11.glTexCoord2f(0.0f, 0.0f);
        GL11.glVertex2i(mX, Engine.getInstance().getDisplayBounds().getY() - mY
                - getHeight());
        GL11.glTexCoord2f(tilingValues.width, 0.0f);
        GL11.glVertex2i(mX + getWidth(), Engine.getInstance().getDisplayBounds().getY()
                - mY - getHeight());
        GL11.glTexCoord2f(tilingValues.width, tilingValues.height);
        GL11.glVertex2i(mX + getWidth(), Engine.getInstance().getDisplayBounds().getY()
                - mY);
        GL11.glEnd(); // Done Building Our Quad (Character)
        GL11.glDisable(GL11.GL_BLEND);

        GL11.glDisable(GL11.GL_TEXTURE_2D);
        mColour.reset();
    }
    
    
    /**
     * 
     * @param mX
     * @param mY
     * @param sizeX
     * @param sizeY
     * @param mU
     * @param mV
     * @param mAlpha
     */
    public void draw(int mX, int mY, int sizeX, int sizeY,float mU, float mV, float mAlpha) {
        
        setWidth(sizeX);
        setHeight(sizeY);
//        GLColour mColour =
//                new GLColour(colour.getRed(), colour.getGreen(), colour.getBlue(),
//                mAlpha);
        colour.setAlpha(mAlpha);
        colour.set();
        GL11.glEnable(GL11.GL_TEXTURE_2D);

        GL11.glDisable(GL11.GL_DEPTH_TEST);
        GL11.glEnable(GL11.GL_BLEND);
        
        //texture.bind();
        GL11.glBegin(GL11.GL_QUADS);
        GL11.glNormal3f(0.0f, 0.0f, 1.0f);
        
        mV = (mV/texture.getHeight());
        mU = (mU/texture.getWidth());
        
        GL11.glTexCoord2f(mU, mV + ((float)h/texture.getHeight()));
        GL11.glVertex2i(mX, Engine.getInstance().getDisplayBounds().getY()
                - mY);
        GL11.glTexCoord2f(mU, mV);
        GL11.glVertex2i(mX, Engine.getInstance().getDisplayBounds().getY() - mY
                - getHeight());
        GL11.glTexCoord2f(mU+((float)w/texture.getWidth()), mV);
        GL11.glVertex2i(mX + getWidth(), Engine.getInstance().getDisplayBounds().getY()
                - mY - getHeight());
        GL11.glTexCoord2f(mU+((float)w/texture.getWidth()), mV+((float)h/texture.getHeight()));
        GL11.glVertex2i(mX + getWidth(), Engine.getInstance().getDisplayBounds().getY()
                - mY);
        GL11.glEnd(); // Done Building Our Quad (Character)
        GL11.glDisable(GL11.GL_BLEND);

        GL11.glDisable(GL11.GL_TEXTURE_2D);
        colour.reset();
    }
    
    /**
     * 
     * @return
     */
    public int getWidth() {
        return w;
    }

    
    /**
     * 
     * @return
     */
    public int getHeight() {
        return h;
    }
    
    /**
     * 
     * @param w
     */
    public void setWidth(int w) {
        this.w = w;
    }

    
    /**
     * 
     * @param h
     */
    public void setHeight(int h) {
        this.h = h;
    }
    
    private void calculateTilingValues() {
        tilingValues = new Dimension();

        if (texture.getWidth() == 0 && texture.getHeight() == 0) {
            tilingValues.width = 1;
            tilingValues.height = 1;
            return;
        }

        tilingValues.width = w / texture.getWidth();
        tilingValues.height = h / texture.getHeight();

        if (tilingValues.width == 0)
            tilingValues.width = 1;
        if (tilingValues.height == 0)
            tilingValues.height = 1;
    }

    

    /**
     * 
     * @param colour
     */
    public void setColour(GLColour colour) {
        this.colour = colour;
    }
    
    /**
     * 
     * @param sizeX
     * @param sizeY
     * @param mAlpha
     */
    public void startBackground(int sizeX, int sizeY, float mAlpha) {
        setWidth(sizeX);
        setHeight(sizeY);
//        GLColour mColour =
//                new GLColour(colour.getRed(), colour.getGreen(), colour.getBlue(),
//                mAlpha);
        colour.setAlpha(mAlpha);
        colour.set();
        GL11.glEnable(GL11.GL_TEXTURE_2D);

        GL11.glDisable(GL11.GL_DEPTH_TEST);
        GL11.glEnable(GL11.GL_BLEND);
        
        //texture.bind();
        GL11.glBegin(GL11.GL_QUADS);
        GL11.glNormal3f(0.0f, 0.0f, 1.0f);
    }

    /**
     * 
     * @param mX
     * @param mY
     * @param mU
     * @param mV
     */
    public void drawTile(int mX, int mY, float mU, float mV) {
        mV = (mV/texture.getHeight());
        mU = (mU/texture.getWidth());
        
        GL11.glTexCoord2f(mU, mV + ((float)h/texture.getHeight()));
        GL11.glVertex2i(mX, Engine.getInstance().getDisplayBounds().getY()
                - mY);
        GL11.glTexCoord2f(mU, mV);
        GL11.glVertex2i(mX, Engine.getInstance().getDisplayBounds().getY() - mY
                - getHeight());
        GL11.glTexCoord2f(mU+((float)w/texture.getWidth()), mV);
        GL11.glVertex2i(mX + getWidth(), Engine.getInstance().getDisplayBounds().getY()
                - mY - getHeight());
        GL11.glTexCoord2f(mU+((float)w/texture.getWidth()), mV+((float)h/texture.getHeight()));
        GL11.glVertex2i(mX + getWidth(), Engine.getInstance().getDisplayBounds().getY()
                - mY);
    }
    
    /**
     * 
     */
    public void stopBackground(){
        GL11.glEnd(); // Done Building Our Quad (Character)
        GL11.glDisable(GL11.GL_BLEND);

        GL11.glDisable(GL11.GL_TEXTURE_2D);
        colour.reset();
    }

    
}
